# Contributor: Antoine Martin (ayakael) <dev@ayakael.net>
# Maintainer: Antoine Martin (ayakael) <dev@ayakael.net>
pkgname=py3-django-cte
#_pkgreal is used by apkbuild-pypi to find modules at PyPI
_pkgreal=django-cte
pkgver=1.3.3
pkgrel=0
pkgdesc="Common Table Expressions (CTE) for Django"
url="https://pypi.python.org/project/django-cte"
arch="noarch"
license="BSD-Clause-3"
depends="py3-django"
checkdepends="py3-pytest postgresql py3-psycopg2 py3-nose"
makedepends="py3-setuptools py3-gpep517 py3-wheel"
source="$pkgname-$pkgver.tar.gz::https://github.com/dimagi/django-cte/archive/refs/tags/v$pkgver.tar.gz"
builddir="$srcdir/$_pkgreal-$pkgver"
subpackages="$pkgname-pyc"

build() {
	gpep517 build-wheel \
		--wheel-dir .dist \
		--output-fd 3 3>&1 >&2
}

check() {
	msg "Setting up test environments"
	export POSTGRES_DB=postgres
	export POSTGRES_USER=postgres
	export POSTGRES_PASSWORD="postgres"

	rm -Rf "$srcdir"/tmp
	initdb -D "$srcdir"/tmp
	postgres -D "$srcdir"/tmp --unix-socket-directories="$srcdir" > "$srcdir"/tmp/psql.log 2>&1 &

	trap "pkill postgres" EXIT
	# wait for psql to be ready
	while ! pg_isready -h "$srcdir" > /dev/null 2>&1; do
		sleep 0.5
	done
	psql -h "$srcdir" -d postgres -c "CREATE ROLE $POSTGRES_USER PASSWORD '$POSTGRES_PASSWORD' INHERIT LOGIN;"
	psql -h "$srcdir" -d postgres -c "ALTER USER $POSTGRES_USER WITH SUPERUSER;"

	python3 -m venv --clear --without-pip --system-site-packages .testenv
	.testenv/bin/python3 -m installer .dist/*.whl

	msg "Running tests"
	.testenv/bin/python3 -m nose -v

	pkill postgres
}

package() {
	python3 -m installer -d "$pkgdir" \
		.dist/*.whl
}

sha512sums="
543ca5d7ae546d00facadaccb23498534f9016ef7f1ce68cf0ca7640e8d0d4bbf0e1354e2d1b02d6787cc51c58098deda5783100ff9b8974fd9abe37ec662402  py3-django-cte-1.3.3.tar.gz
"
